import {
  reqDeviceByImei,
  reqConsumerDeviceAdd,
  reqConsumerDeviceUpdate
} from '../../../../request/api'
import {
  showMsg
} from '../../../../utils/config';
const app = getApp()

Page({
  data: {
    dataList: [{
      id: 0,
      name: '我的家',
    }, {
      id: 2,
      name: '深圳家',
    }, {
      id: 3,
      name: '广州家',
    }, {
      id: 4,
      name: '珠海家',
    }, {
      id: 5,
      name: '佛山家',
    }, ],
    imei: "",
    showInputDialog: false,
    deviceCode: ""
  },

  onLoad(options) {

  },


  onShow() {

  },
  // 判断imei是否已存在
  async getDeviceByImei(imei) {
    // const resle = await reqDeviceByImei(imei)
    // console.log(resle, 'resle');
    // if (resle.code !== 20000) return showMsg(resle.message)
    // let deviceName = ''
    // if (resle.data.row.deviceType == "SmokeAlarm") {
    //   deviceName = '烟感探测报警器'
    // } else if (resle.data.row.deviceType == "GasSensor") {
    //   deviceName = '智能燃气报警器'
    // } else if (resle.data.row.deviceType == "Infrared") {
    //   deviceName = '红外探测器'
    // } else if (resle.data.row.deviceType == "FireHydrant") {
    //   deviceName = '消防水压'
    // } else if (resle.data.row.deviceType == "TemperatureAlarm") {
    //   deviceName = '温感火灾探测报警器'
    // } else if (resle.data.row.deviceType == "WaterSensor") {
    //   deviceName = '水浸探测器'
    // } else if (resle.data.row.deviceType == "DoorMagnetism") {
    //   deviceName = '门磁'
    // } else if (resle.data.row.deviceType == "Camera") {
    //   deviceName = '摄像机'
    // } else if (resle.data.row.deviceType == "PressureGauge") {
    //   deviceName = "无线数字压力表";
    // } else if (resle.data.row.deviceType == "TankGauge") {
    //   deviceName = "无线数字液位表";
    // } else if (resle.data.row.deviceType == "UserTransferDevice") {
    //   deviceName = "用户信息传输装置";
    // } else if (resle.data.row.deviceType == "ElectricalMeasurement") {
    //   deviceName = "电气综合测控装置";
    // } else if (resle.data.row.deviceType == "SoundLightVoiceAlarm") {
    //   deviceName = "声光报警器";
    // } else if (resle.data.row.deviceType == "FireAlarmController") {
    //   deviceName = "火灾报警控制器";
    // } else if (resle.data.row.deviceType == "FaultArc") {
    //   deviceName = "故障电弧探测器";
    // }

    const res = await reqConsumerDeviceAdd({
      deviceCode: imei,
      deviceName: '烟感探测报警器',
      userId: app.globalData.userId,
      homeId: app.globalData.familyInfo.id,
      roomId: ''
    })
    if (res.code != 20000) return showMsg(res.message)
    console.log(res, 'res');
    // 修改产品名称，默认帮用户赋值 设备名称=产品名称
    const resle = await reqConsumerDeviceUpdate({
      deviceCode:imei,
      deviceName:res.data.productName,
      roomId:''
    })
    if (resle.code !== 20000) return showMsg(resle.message)
    console.log(res,'res');
    app.globalData.isUpdateCIndex = true
    wx.setStorageSync('isUpdateCIndex', true);
    this.setData({
      showInputDialog: false
    })
    wx.navigateTo({
      url: `/packageC/pages/device/roomSet/roomSet?deviceCode=${imei}&deviceName=${res.data.productName}&pic=${res.data.pic}`,
    })
  },
  onAddRoom() {
    this.setData({
      showInputDialog: true
    })
  },
  inputDialogHandle(e) {
    console.log(e);
    const {
      actionType,
      value
    } = e.detail
    if (actionType === 'confirm') {
      if (!value) return showMsg("请输入设备编码")
      this.getDeviceByImei(value) // 判断imei是否已存在
    } else {
      this.setData({
        showInputDialog: false
      })
    }
  },

  /**
   * @name:  扫码imei
   * @msg: 掉接口时记得询问清楚截取方法
   * @return {*}
   */
  imeiScan(e) {
    let that = this
    let {
      id
    } = e.target.dataset
    let qrcode = ''
    wx.scanCode({
      success(res) {
        console.log(res);
        qrcode = res.result;
        if (!qrcode) return showMsg('扫描失败，请重新扫描')
        if (qrcode.length >= 3) {
          if (that.isjson(qrcode)) {
            const result = JSON.parse(qrcode);
            // JSON字符格式
            that.setData({
              searchValue: result.ID, //威视达康摄像机取值，后续看需求更改 lc
            })
            that.getDeviceByImei(result.ID) // 判断imei是否已存在
          } else {
            // 非JSON格式
            that.setData({
              searchValue: qrcode,
            })
            that.getDeviceByImei(qrcode) // 判断imei是否已存在
          }
        } else {
          showMsg('扫描失败，请重新扫描')
          return
        }
      }
    })
  },
  // js判断字符串是否为JSON格式
  isjson(str) {
    if (typeof str == 'string') {
      try {
        var obj = JSON.parse(str);
        if (typeof obj == 'object' && obj) {
          return true;
        } else {
          return false;
        }

      } catch (e) {
        console.log('error：' + str + '!!!' + e);
        return false;
      }
    }
    console.log('It is not a string!')
  },
})